varying vec3 worldNormal;
varying vec3 position;
varying vec4 pos;
uniform vec3 lightPos;
uniform vec3 eyePos;
uniform sampler2D envMap;
uniform sampler2D normalMap;
uniform sampler2D envMap2;
varying float timer;
const vec2 off1 = vec2(2.0,1.0);
const vec2 off2 = vec2(1.0,3.0);


vec3 hcount(vec3 l, vec3 v) {
    float module = sqrt(pow(l.x+v.x,2.0) + pow(l.y+v.y,2.0) + pow(l.z+v.z,2.0));
    return (l+v)/module;
}

float dist(vec3 start, vec3 end) {
    vec3 vect = start - end;
    return sqrt(vect.x*vect.x + vect.y*vect.y + vect.z*vect.z)/10.0;
}

float module(vec3 a, vec3 b) {
    vec3 c = a - b;
    return sqrt(c.x*c.x + c.y*c.y + c.z*c.z);
}

#extension GL_EXT_gpu_shader4 : enable


float Noise(float x, float y ,float z) {
    float n = 2.0*z + x + y * 57.0;
    n = float(int(( ((4.0*(n-z) * (n/2.0 * (z + 15731.0) + 789721.0) + 43589.0)) / 107374.0))%1000);
    n /= 1000.0;
    return n;
}

void main() {
    vec3 worldnormal = worldNormal;
    vec4 bump = texture2D(normalMap,pos.xy/200.0 + timer*off1/2.0) + texture2D(normalMap,pos.xy/200.0 + timer*off2/2.0);
   // worldnormal += bump.xyz/5.0;
    float distscale = 0.1;
    vec2 offset;
    offset.x = -worldnormal.x*distscale;
    offset.y =  -worldnormal.y*distscale;
    vec3 n = worldnormal;
     vec3 l = normalize(lightPos - position);
     float r = max(0.5,dot(l, n));
     vec3 v = normalize(eyePos - position);
     v.z = abs(v.z);
     float r2 = 0.0;//pow(max(dot(n,normalize(l+v)), 0.0),900.0);
     vec2 coor;
     vec4 ver = pos;
     ver.w = 1.0;
     vec4 pos2 = gl_ModelViewProjectionMatrix * ver;
     coor.x = (pos2.x / pos2.w) + 1.0;
     coor.y = (pos2.y / pos2.w) + 1.0;
     float cosQ = dot(n, v)/(dot(n,n)*dot(v,v));
     float fresnel = 1.0 - cosQ;
     vec4 color;
     vec4 sum = vec4(0.3,0.3,0.3,1.0);
     sum.x = sum.x - abs(1.0/4.0);
     sum.y = sum.y - abs(1.0/6.0);
     sum.z = sum.z - abs(1.0/10.0);
     vec3 l2 = normalize(lightPos - eyePos);
     float rr = dot(l2,vec3(0.0,0.0,1.0));
     if(lightPos.z < 0.0) rr = 0;
     sum = sum*rr;
     vec2 summary = (coor+offset)/2.0;//  
     if(summary.x > 1.0) summary.x = 0.0;
     if(summary.y > 1.0) summary.y = 0.0;
     if(summary.x < 0.0) summary.x = 1.0;
     if(summary.y < 0.0) summary.y = 1.0;
     if(eyePos.z > pos.z)
         color = (texture2D(envMap, summary)*r)*fresnel + (texture2D(envMap2, summary)*r)*(1.0-fresnel) + vec4(0.3,0.3,0.1,1.0)*r2;
     else color = (sum)*(fresnel)/(1.0+abs(eyePos.z/10.0)) + (texture2D(envMap2, summary)*r)*(1.0-fresnel) + vec4(0.3,0.3,0.1,1.0)*r2;

     gl_FragColor = color;
     vec4 summar = texture2D(envMap2, summary);
     float summ = summar.x + summar.y + summar.z;
     /*if(summ > 1.5) {
         float differ = 0.01;
         float level = max(0.0,4.0 - summ);
         level *= level;
         vec4 left_sum = texture2D(envMap2, summary-vec2(0.0005));
         float left = left_sum.x+left_sum.y+left_sum.z;
         vec4 right_sum = texture2D(envMap2, summary+vec2(0.0005));
         float right = right_sum.x+right_sum.y+right_sum.z;
         vec4 deep_left_sum = texture2D(envMap2, summary-vec2(0.001));
         float deep_left = deep_left_sum.x+deep_left_sum.y+deep_left_sum.z;
         vec4 deep_right_sum = texture2D(envMap2, summary+vec2(0.001));
         float deep_right = deep_right_sum.x+deep_right_sum.y+deep_right_sum.z;
         if((summ+right+deep_right)/3.0 > (left+differ*2.0+deep_left)/2.0 ) {
             float df = (summ-left-0.05)*1.5;
             gl_FragColor -= vec4(0.5,0.5,0.5,0.0)*df/level;
             gl_FragColor += vec4(0.7,0.3,0.0,0.0)*df/level;
         }
         else if((summ+left+deep_left)/3.0 > (right+differ*2.0+deep_right)/2.0) {
             float df = (summ-right-0.05)*1.5;
             gl_FragColor -= vec4(0.5,0.5,0.5,0.0)*df/level;
             gl_FragColor += vec4(0.0,0.3,0.7,0.0)*df/level;
         }
     }*/
     if(eyePos.z > pos.z) {
     float fr = module(eyePos,pos.xyz);
     fr = max(1.0,fr/300.0);
     float fog = min(max( dot(normalize(lightPos), vec3(0.0,0.0,1.0)), 0.1 ),0.5);
     gl_FragColor.x = fog + (gl_FragColor.x - fog)/fr;
     gl_FragColor.y = fog + (gl_FragColor.y - fog)/fr;
     gl_FragColor.z = fog + (gl_FragColor.z - fog)/fr;
     }
}